package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleInfoService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threablocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2021/7/15 16:11
 **/
@Service
public class ApArticleInfoServiceImpl implements ApArticleInfoService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        // 1. 校验参数
        // 2. 准备默认的返回值
        boolean isfollow = false,islike = false,isunlike = false,iscollection = false;
        // 3. 判断有没有登录
        ApUser user = AppThreadLocalUtils.getUser();
        if(user!=null){
            // 3.1 如果登录 查询相关行为
            // 查询行为实体
            ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            if(behaviorEntry == null){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"行为实体数据不存在");
            }
            Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            // 是否点赞
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if(likesBehavior!=null){
                islike = true;
            }
            // 是否收藏
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            if(apCollection!=null){
                iscollection = true;
            }
            // 是否不喜欢
            ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if(unlikesBehavior!=null){
                isunlike = true;
            }
            // 是否关注
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
            if(score != null){
                isfollow = true;
            }
        }
        // 3.2  如果未登录
        // 4. 封装返回结果   4个boolean变量存入map
        Map map = new HashMap<>();
        map.put("isfollow",isfollow);
        map.put("islike",islike);
        map.put("isunlike",isunlike);
        map.put("iscollection",iscollection);
        return ResponseResult.okResult(map);
    }
}
